package com.amap.rideroute.util;

import android.util.Log;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;

import java.util.ArrayList;
import java.util.List;

/**
 * 点距离排序
 */
public class SortPathUtil {
    /**
     * 对点进行距离排序,返回两两需要绘制距离的点数组的集合
     * @return
     */
    public static List<LatLonPoint[]> sortStraightPath(LatLonPoint currentLocation,List<LatLonPoint> points){
        //计算当前用户与集合中点的距离，找出最短距离
        List<LatLonPoint[]> resultPoints = new ArrayList<>();
        getShortDistancePointArgs(currentLocation,points,resultPoints);
        Log.e("集合",resultPoints.toString());
        return resultPoints;
    }
    private static void getShortDistancePointArgs(LatLonPoint flagPoint,List<LatLonPoint> points,List<LatLonPoint[]> resultPoints){
        LatLonPoint[] shortPointArgs = new LatLonPoint[2];
        LatLonPoint shortPoint = null;
        float distance = -1;
        LatLng latLngStart = new LatLng(flagPoint.getLatitude(),flagPoint.getLongitude());
        LatLng latLngEnd;
        for (LatLonPoint point : points) {
            latLngEnd = new LatLng(point.getLatitude(),point.getLongitude());
            float tempDistance = AMapUtils.calculateLineDistance(latLngStart, latLngEnd);
            //找出最短的距离
            if(distance == -1){
                distance = tempDistance;
                shortPoint = point;
                Log.e("最短距离A",tempDistance + "");
            }else if(tempDistance < distance){
                distance = tempDistance;
                shortPoint = point;
                Log.e("最短距离B",tempDistance + "");
            }
            Log.e("最短距离",tempDistance + "");
        }
        shortPointArgs[0] = flagPoint;
        shortPointArgs[1] = shortPoint;
        resultPoints.add(shortPointArgs);
        points.remove(shortPoint);
        if(points.size() > 0){
            getShortDistancePointArgs(shortPoint,points,resultPoints);
        }
    }
}
